{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## series_to_supervised()函数\n",
    "series_to_supervised()函数，可以接受单变量或多变量的时间序列，将时间序列数据集转换为监督学习任务的数据集。参数如下:\n",
    "\n",
    "data：一个list集合或2D的NumPy array\n",
    "n_in：作为输入X的滞后观察数量，取值为[1,...,len(data)]，默认为1\n",
    "n_out：作为输出观察数量，取值为[0..len(data)-1]，默认为1\n",
    "dropnan：是否删除空值（即Nan）的行，取值为True或False，默认为True\n",
    "https://zhuanlan.zhihu.com/p/399763839"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pandas import DataFrame\n",
    "from pandas import concat\n",
    "\n",
    "def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):\n",
    "\t\"\"\"\n",
    "\tFrame a time series as a supervised learning dataset.\n",
    "\tArguments:\n",
    "\t\tdata: Sequence of observations as a list or NumPy array.\n",
    "\t\tn_in: Number of lag observations as input (X).\n",
    "\t\tn_out: Number of observations as output (y).\n",
    "\t\tdropnan: Boolean whether or not to drop rows with NaN values.\n",
    "\tReturns:\n",
    "\t\tPandas DataFrame of series framed for supervised learning.\n",
    "\t\"\"\"\n",
    "\tn_vars = 1 if type(data) is list else data.shape[1]\n",
    "\tdf = DataFrame(data)\n",
    "\tcols, names = list(), list()\n",
    "\t# input sequence (t-n, ... t-1)\n",
    "\tfor i in range(n_in, 0, -1):\n",
    "\t\tcols.append(df.shift(i))\n",
    "\t\tnames += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]\n",
    "\t# forecast sequence (t, t+1, ... t+n)\n",
    "\tfor i in range(0, n_out):\n",
    "\t\tcols.append(df.shift(-i))\n",
    "\t\tif i == 0:\n",
    "\t\t\tnames += [('var%d(t)' % (j+1)) for j in range(n_vars)]\n",
    "\t\telse:\n",
    "\t\t\tnames += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]\n",
    "\t# put it all together\n",
    "\tagg = concat(cols, axis=1)\n",
    "\tagg.columns = names\n",
    "\t# drop rows with NaN values\n",
    "\tif dropnan:\n",
    "\t\tagg.dropna(inplace=True)\n",
    "\treturn agg"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  单变量多步时间序列预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   var1(t-4)  var1(t-3)  var1(t-2)  var1(t-1)  var1(t)  var1(t+1)  var1(t+2)  \\\n",
      "4        0.0        1.0        2.0        3.0        4        5.0        6.0   \n",
      "\n",
      "   var1(t+3)  var1(t+4)  var1(t+5)  \n",
      "4        7.0        8.0        9.0  \n"
     ]
    }
   ],
   "source": [
    "values = [x for x in range(10)]\n",
    "data = series_to_supervised(values, 4, 6)\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 多变量多步时间序列预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   var1(t-9)  var2(t-9)  var1(t-8)  var2(t-8)  var1(t-7)  var2(t-7)  \\\n",
      "9        0.0       50.0        1.0       51.0        2.0       52.0   \n",
      "\n",
      "   var1(t-6)  var2(t-6)  var1(t-5)  var2(t-5)  var1(t-4)  var2(t-4)  \\\n",
      "9        3.0       53.0        4.0       54.0        5.0       55.0   \n",
      "\n",
      "   var1(t-3)  var2(t-3)  var1(t-2)  var2(t-2)  var1(t-1)  var2(t-1)  var1(t)  \\\n",
      "9        6.0       56.0        7.0       57.0        8.0       58.0        9   \n",
      "\n",
      "   var2(t)  \n",
      "9       59  \n"
     ]
    }
   ],
   "source": [
    "raw = DataFrame()\n",
    "raw['ob1'] = [x for x in range(10)]\n",
    "raw['ob2'] = [x for x in range(50, 60)]\n",
    "values = raw.values\n",
    "data = series_to_supervised(values, 9, 1)#想达到matlab效果，需要输入输出等于数据个数\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 现在呢，咱们需要做的是一个单变量多步时间预测的一个事情"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "ea70d47c1026074199160e48050b9f1be75d4ba7d6f2081b88238ca77789226b"
  },
  "kernelspec": {
   "display_name": "Python 3.7.11 ('pytorch-gpu')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
